﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LuDo.PitEditor
{
    public partial class Reporting : Form
    {
        public Reporting(string ConnectionString)
        {
            InitializeComponent();
            cmbPeriod.SelectedIndex = PitEditor.Properties.Settings.Default.DefaultReportTimeIndex;
            this.articoliTA.Connection.ConnectionString = ConnectionString;
        }

        private void Reporting_Load(object sender, EventArgs e)
        {
            UpdateView();         
        }

        private void cmbPeriod_SelectedIndexChanged(object sender, EventArgs e)
        {         
            switch (cmbPeriod.SelectedIndex)
            {
                case 0:     //Settimana in corso
                    DayOfWeek day = DateTime.Today.DayOfWeek;
                    int d = (int)day;
                    int offset = d == 0 ? 7 : d - 1;
                    Date1.Value = DateTime.Today.AddDays(-offset);
                    break;
                case 1:     //Mese in corso
                    Date1.Value = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
                    break;
                case 2:     //Anno in corso
                    Date1.Value = new DateTime(DateTime.Today.Year, 1, 1);
                    break;
                case 3:     //Ultima settimana
                    Date1.Value = DateTime.Today.AddDays(-7);
                    break;
                case 4:     //Ultime 2 settimane
                    Date1.Value = DateTime.Today.AddDays(-14);
                    break;
                case 5:     //Ultimo mese
                    Date1.Value = DateTime.Today.AddDays(-30);
                    break;
                case 6:     //Ultimi 2 mesi
                    Date1.Value = DateTime.Today.AddDays(-60);
                    break;
                case 7:     //Ultimi 3 mesi
                    Date1.Value = DateTime.Today.AddDays(-90);
                    break;
                case 8:     //Ultimi 6 mesi
                    Date1.Value = DateTime.Today.AddDays(-180);
                    break;
                case 9:     //Ultimo anno
                    Date1.Value = DateTime.Today.AddDays(-365);
                    break;
                case 10:     //Tutti i tempi
                    Date1.Value = new DateTime(1999, 8, 1);
                    break;
                default:
                    break;
            }
            Date2.Value = DateTime.Today;
        }

        private void btnEsegui_Click(object sender, EventArgs e)
        {
            if (Date1.Value < Date2.Value)
            {
                CaricaDati();

                UpdateView();

                int TotalLength = this.articoliDS.Articoli.Sum(c => c.Testo.Length);
                int numb = this.articoliDS.Articoli.Count;

                if (numb > 0)
                {
                    decimal dim = CalcolaDimensioneFile(TotalLength);
                    decimal lordo = CalcolaLordo(TotalLength);
                    decimal netto = CalcolaNetto(TotalLength);
                    lblDimensione.Text = String.Format("{0:N2} KB", dim);
                    lblLordo.Text = String.Format("{0:C2}", lordo);
                    lblNetto.Text = String.Format("{0:C2}", netto);

                    lblAvg1.Text = String.Format("{0:N2} KB", dim / numb);
                    lblAvg2.Text = String.Format("{0:C2}", lordo / numb);
                    lblAvg3.Text = String.Format("{0:C2}", netto / numb);
                }
            }
            else
            {
                string msg = "Errore di impostazione date.";
                MessageBox.Show(msg);
            }
        }

        private void UpdateView()
        {
            int n = this.articoliDS.Articoli.Count;
            gridArticoli.Visible = n > 0;
            pnlSum.Visible = n > 0;
        }

        private void CaricaDati()
        {
            DateTime date1 = new DateTime(Date1.Value.Year, Date1.Value.Month, Date1.Value.Day, 0, 0, 0);
            DateTime date2 = new DateTime(Date2.Value.Year, Date2.Value.Month, Date2.Value.Day, 23, 59, 59);
            this.articoliTA.FillByPeriod(this.articoliDS.Articoli, date1, date2);
        }

        private void gridArticoli_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
            DataGridViewColumn col = gridArticoli.Columns[e.ColumnIndex];
            if (col == ColumnDimensione || col == ColumnLordo || col == ColumnNetto)
            {
                DataGridViewRow row = gridArticoli.Rows[e.RowIndex];
                DataRowView view = (DataRowView)row.DataBoundItem;
                articoliDS.ArticoliRow art = (articoliDS.ArticoliRow)view.Row;
                if (col == ColumnDimensione)
                {
                    decimal dim = CalcolaDimensioneFile(art.Testo.Length);
                    e.Value = String.Format("{0:N2} KB", dim);
                }
                else if (col == ColumnLordo)
                {
                    decimal lordo = CalcolaLordo(art.Testo.Length);
                    e.Value = String.Format("{0:C2}", lordo);
                }
                else if (col == ColumnNetto)
                {
                    decimal netto = CalcolaNetto(art.Testo.Length);
                    e.Value = String.Format("{0:C2}", netto);
                }
            }

        }

        private decimal CalcolaDimensioneFile(int LunghezzaTesto)
        {
            float dim = (float)LunghezzaTesto / 1024F;
            decimal d = Convert.ToDecimal(dim);
            return d;
        }
        private decimal CalcolaLordo(int LunghezzaTesto)
        {
            decimal quota = PitEditor.Properties.Settings.Default.DimensioneQuota;
            decimal lordo = PitEditor.Properties.Settings.Default.LordoQuota;
            decimal dim = CalcolaDimensioneFile(LunghezzaTesto);
            decimal l = lordo * dim / quota;
            return l;
        }
        private decimal CalcolaNetto(int LunghezzaTesto)
        {
            decimal n = CalcolaLordo(LunghezzaTesto) / 1.2M;
            return n;
        }    
    }
}
